<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/common.css">
    <link rel="stylesheet" href="../css/ui-base.css">
    <script src="../js/jquery-1.10.1.min.js"></script>
    <script src="../js/request.js"></script>
    <script src="../js/cookie.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        li {
            list-style: none;
        }

        img {
            vertical-align: top;
        }

        .wrap {
            width: 1140px;
            margin: 0 auto;
            margin-top: 100px;
            position: relative;
            overflow: hidden;
            height: 600px;
        }

        .wrapBox {
            width: 400px;
            height: 400px;
            background-color: pink;
            position: relative;
        }

        .wrapBox img {
            width: 100%;
            height: 100%;
        }

        .shadow {
            width: 200px;
            height: 200px;
            background-color: rgba(0, 0, 0, 0.6);
            position: absolute;
            left: 0;
            top: 0;

            display: none;
        }

        .showBigImg {
            width: 400px;
            height: 400px;
            float: left;
            background-color: #0000004d;
            overflow: hidden;
            position: absolute;
            left: 500px;
            top: 0;

            display: none;

        }

        .showBigImg img {
            position: absolute;
            /* z-index: -1; */
            width: 800px;
            height: 800px;
        }

        .switchList {
            width: 400px;
            margin-top: 20px;
            display: flex;
        }

        .switchList li {
            flex: 1;
            margin: 5px;
            border: 1px solid transparent;
        }

        .switchList .active {
            border-color: red;
        }

        .switchList li img {
            width: 100%;
        }

        .wrap .left {
            float: left;
        }

        .wrap .right {
            float: left;
            /* overflow: hidden; */
            width: 640px;
            margin-left: 100px;
            background-color: #6ff;
            min-height: 400px;
        }

        .wrap .right h2 {
            font-size: 18px;
            margin-bottom: 20px;
        }

        .wrap .right>div {
            margin-bottom: 20px;
        }

        .detailBox {
            width: 1140px;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <div class="box">
        <a href="./login.html">你好,请登录</a>
        <a href="./register.html">注册</a>
    </div>
    <div class="wrap">
        <div class="left">
            <!-- <div class="wrapBox">
                <div class="shadow"></div>
                <img class="smallImg" src="../images/small.jpg" alt="">
            </div>
            <ul class="switchList">
                <li class="active"><img src="../images/small.jpg" bigImg="../images/big.jpg" alt=""></li>
                <li><img src="../images/girlsmall1.jpg" bigImg="../images/girlbig1.jpg" alt=""></li>
                <li><img src="../images/girlsmall2.jpg" bigImg="../images/girlbig2.jpg" alt=""></li>
                <li><img src="../images/girlsmall3.jpg" bigImg="../images/girlbig3.jpg" alt=""></li>
                <li><img src="../images/girlsmall4.jpg" bigImg="../images/girlbig4.jpg" alt=""></li>
            </ul>
            <div class="showBigImg">
                <img class="bigImg" src="../images/big.jpg" alt="">
            </div> -->
        </div>
        <div class="right">
            <!-- <h2>Apple iPhone 11 (A2223) 128GB 黑色 移动联通电信4G手机 双卡双待</h2>
            <div class="price">
                京 东 价:￥ <span>4999.00</span> 降价通知
            </div>
            <div class="buyNum">
                <span class="reduce">-</span>
                <input type="number" value="1">
                <span class="add">+</span>
            </div>
            <button id="addToCar">
                加入购物车
            </button> -->
        </div>
    </div>
    <div class="detailBox">

    </div>
</body>
<script>


    if (getCookie("logUser")) {
        var user = getCookie("logUser");
        $(".box").html(`<a href="javascript:;">欢迎,${user}</a><button class="exit">退出</button>`);

        var button = document.getElementsByClassName("exit")[0];
        button.onclick = function () {
            setCookie("logUser", "", -1);
            location.reload();
        }
    }


    // 为了用户体验  等页面所有内容加载完毕之后执行 

    var search = location.search;
    if (search) {
        var gid = search.split("=")[1];
        console.log(gid);

        searchGoodsById({ gid: gid }).then(function (data) {
            console.log(data);
            var { id, goodsId, goodsName, goodsPrice, smallPicList, bigPicList, goodsDetail } = data;

            // 左边放大镜的动态生成
            var leftHTML = `
            <div class="wrapBox">
                <div class="shadow"></div>
                <img class="smallImg" src="${bigPicList[0]}" alt="">
            </div>
            <ul class="switchList">
                
            </ul>
            <div class="showBigImg">
                <img class="bigImg" src="${bigPicList[0]}" alt="">
            </div>`;

            var leftSwitchHTML = "";
            //     <li class="active"><img src="../images/small.jpg" bigImg="../images/big.jpg" alt=""></li>
            //     <li><img src="../images/girlsmall1.jpg" bigImg="../images/girlbig1.jpg" alt=""></li>
            //     <li><img src="../images/girlsmall2.jpg" bigImg="../images/girlbig2.jpg" alt=""></li>
            //     <li><img src="../images/girlsmall3.jpg" bigImg="../images/girlbig3.jpg" alt=""></li>
            //     <li><img src="../images/girlsmall4.jpg" bigImg="../images/girlbig4.jpg" alt=""></li>

            var len = smallPicList.length > 5 ? 5 : smallPicList.length;
            for (var i = 0; i < len; i++) {
                leftSwitchHTML += `<li class="${i == 0 ? 'active' : ''}"><img src="${bigPicList[i]}" bigImg="${bigPicList[i]}" alt=""></li>`
            }


            $(".wrap .left").html(leftHTML);
            $(".wrap .left .switchList").html(leftSwitchHTML);

            // 右边商品新的的生成
            var rightHTML = ` <h2>${goodsName}</h2>
            <div class="price">
                京 东 价:￥ <span>${goodsPrice}</span> 降价通知
            </div>
            <div class="buyBox">
                <span class="reduce">-</span>
                <input class="buyNum" type="number" value="1">
                <span class="add">+</span>
            </div>
            <button id="addToCar">
                加入购物车
            </button>`;
            $(".wrap .right").html(rightHTML);


            // 生成详情信息
            $(".detailBox").html(goodsDetail);

            // 1. 等ajax请求完毕  动态生成之后再获取元素绑定事件
            // $("#addToCar").click(function () {
            //     console.log("加入购物车");
            // })


        }).catch(err => {
            throw err;
        })

    } else {
        location.href = "./goodsList.html";
    }


    // 加入购物车需要知道什么信息?

    // 1. 谁买的?   =>每个人都有账号
    // 2. 买了什么东西?   => 每一件商品都有 对应的编号(gid)
    // 3. 买了几件?     =>   

    // 谁  买了什么东西  买了多少件?


    // 2. 利用事件委托 给未来生成的元素绑定事件
    $(document).on("click", "#addToCar", function () {
        // console.log("假如购物车");
        // 点击时验证 是否登录
        var user = getCookie("logUser");
        if (user) {  //用户已登录
            console.log("加入购物车");
            var buyNum = $(".buyNum").val();
            addToShoppingCar({ user, gid, buyNum }).then(function (data) {
                console.log(data);
                if (data.status) {
                    if (confirm("商品加入成功,是否进入购物车?")) {
                        location.href = "./shoppingCar.html";
                    }
                } else {
                    alert(data.msg);
                }
            })

        } else { //  用户没有登录  =>跳转登录页(记录当前页面是哪个页面(编码)=>登录成功之后(解码)返回)
            location.href = "./login.html?returnUrl=" + encodeURIComponent(location.href);
        }


    })





</script>

</html>